﻿using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace Bootstrap.Security
{
    internal class BootstrapAdminHttpClient
    {
        private readonly HttpClient _client;

        public BootstrapAdminHttpClient(HttpClient client) => _client = client;

        public async Task<T> PostAsJsonAsync<T>(string requestUri, object postData)
        {
            var ret = default(T);
            try
            {
                var sc = new StringContent(JsonConvert.SerializeObject(postData));
                sc.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var result = await _client.PostAsync(requestUri, sc);
                if (result.IsSuccessStatusCode)
                {
                    var content = await result.Content.ReadAsStringAsync();
                    ret = JsonConvert.DeserializeObject<T>(content);
                }
            }
            catch { }
            return ret;
        }
    }
}
